package com.aicheyoudao;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.Toast;

import com.aicheyoudao.utils.NavigationUtil;
import com.aicheyoudao.utils.SearchUtils;
import com.aicheyoudao.utils.SearchUtils.searchResListener;
import com.baidu.location.BDLocation;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.navisdk.BNaviPoint;

public class NearSearchResActivity extends Activity implements searchResListener, OnClickListener {
	private SearchUtils search;
	private BDLocation mLocation;
	private SearchResListAdapter adapter;
	private ArrayList<PoiInfo> listData;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.near_search_res);

		search = SearchUtils.getSearchUtils();
		search.setsearchResListener(this);
		mLocation = MyApplication.getInstance().getMlocation();
		initView();
		String searchKey = getIntent().getStringExtra("searchkey");
		search.PoiNearSearch(new LatLng(mLocation.getLatitude(), mLocation.getLongitude()), searchKey);
		MyLoadingDialog.StartShowProgressDialog(this);
	}

	private void initView() {
		ListView list = (ListView) findViewById(R.id.nearsearch_list);
		listData = new ArrayList<PoiInfo>();
		adapter = new SearchResListAdapter(this, listData, new LatLng(mLocation.getLatitude(), mLocation.getLongitude()), this);
		list.setAdapter(adapter);
	}

	@Override
	public void searchSucess(PoiResult res) {
		MyLoadingDialog.stopDialog();
		List<PoiInfo> data = res.getAllPoi();
		listData.clear();
		listData.addAll(data);
		adapter.notifyDataSetChanged();
	}

	@Override
	public void onClick(View v) {
		int position = (Integer) v.getTag();
		switch (v.getId()) {
		case R.id.search_res_shopdetails:
			Toast.makeText(NearSearchResActivity.this, listData.get(position).name, Toast.LENGTH_SHORT).show();
			break;
		case R.id.search_res_cell:
			String phoneNum = listData.get(position).phoneNum;
			if (!"".equals(phoneNum) && phoneNum != null) {
				Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse("tel:" + phoneNum));
				startActivity(intent);
			} else {
				Toast.makeText(NearSearchResActivity.this, "无法获取电话号码", Toast.LENGTH_SHORT).show();
			}

			break;
		case R.id.search_res_navigation:
			PoiInfo info = listData.get(position);

			if (MyApplication.getInstance().ismIsEngineInitSuccess()) {
				BDLocation mlocation = MyApplication.getInstance().getMlocation();
				BNaviPoint start = new BNaviPoint(mlocation.getLongitude(), mlocation.getLatitude(), mlocation.getAddrStr(),
						BNaviPoint.CoordinateType.BD09_MC);
				BNaviPoint end = new BNaviPoint(info.location.longitude, info.location.latitude, info.address, BNaviPoint.CoordinateType.BD09_MC);
				NavigationUtil.start(NearSearchResActivity.this, start, end);
			}
			break;

		}

	}
}
